package com.xzit.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzit.rental.entity.AutoInfo;
import com.xzit.rental.entity.Maintain;
import com.xzit.rental.service.IAutoInfoService;
import com.xzit.rental.service.IMaintainService;
import com.xzit.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何时说晚安
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/rental/maintain")
public class MaintainController {

    @Resource
    IMaintainService maintainService;

    @Resource
    IAutoInfoService autoInfoService;

    @PostMapping("{start}/{size}")
    public Result search(@PathVariable("start") int start,
                         @PathVariable("size") int size,
                         @RequestBody Maintain maintain) {
        Page<Maintain> page = new Page<>(start, size);
        return Result.success().setData(maintainService.searchByPage(page,maintain));
    }
    @PostMapping("")
    public Result save(@RequestBody Maintain maintain) {
        // 设置关联的车辆信息id
        AutoInfo autoInfo = autoInfoService.selectByAutoNum(maintain.getAutoNum());
        maintain.setAutoId(autoInfo.getId());
        // 将车辆实保次数加一
        autoInfo.setActualNum(autoInfo.getActualNum() + 1);
        // 更新车辆信息
        autoInfoService.updateById(autoInfo);
        return maintainService.save(maintain)?Result.success():Result.fail();
    }
    @PutMapping("")
    public Result update(@RequestBody Maintain maintain) {
        AutoInfo autoInfo = autoInfoService.selectByAutoNum(maintain.getAutoNum());
        maintain.setAutoId(autoInfo.getId());
        return maintainService.updateById(maintain)?Result.success():Result.fail();
    }
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable("ids") String ids) {
        return maintainService.delete(ids)?Result.success():Result.fail();
    }


}
